import * as CryptoJS from 'crypto-js'

export const getAssetsImages = (name: string) => {
  return new URL(`/src/assets/img/${name}`, import.meta.url).href
}
/**
 *加密处理
 */
 export const encryption = (params) => {
  let {
    data,
    type,
    param,
    key
  } = params
  const result = JSON.parse(JSON.stringify(data))
  if (type === 'Base64') {
    param.forEach(ele => {
      result[ele] = btoa(result[ele])
    })
  } else {
    param.forEach(ele => {
      var data = result[ele]
      key = CryptoJS.enc.Latin1.parse(key)
      var iv = key
      // 加密
      var encrypted = CryptoJS.AES.encrypt(
        data,
        key, {
          iv: iv,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.ZeroPadding
        })
      result[ele] = encrypted.toString()
    })
  }
  return result
}
/**
 *解密处理
 */
 export const Decrypt = (params) => {
  let {
    data,
    type,
    param,
    key
  } = params
  const result = JSON.parse(JSON.stringify(data))
  if (type === 'Base64') {
    param.forEach(ele => {
      result[ele] = btoa(result[ele])
    })
  } else {
    param.forEach(ele => {
      var data = result[ele]
      key = CryptoJS.enc.Latin1.parse(key)
      var iv = key
      // 加密
      var encrypted = CryptoJS.AES.decrypt(
        data,
        key,
        {
          iv: iv,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.ZeroPadding
        })
      result[ele] = encrypted.toString(CryptoJS.enc.Utf8)
    })
  }
  return result
}

export function getUrlKey (name:any,url?:string) {
  let str = (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url ? url : location.href) || [, ""])[1].replace(/\+/g, '%20')
  return decodeURIComponent(str) || null
}
interface datesType {
  wloopValue?:Array<string|number>,
  mloopValue?:Array<string|number>,
  effectTime:Date,
  loopType:string
}
export function timeToCron(dates:datesType) {
  let m = '' as any
  let s = '' as any
  let h = '' as any
  let w = dates.wloopValue || []
  let mo = dates.mloopValue || []
  if (dates.effectTime) {
    h = dates.effectTime.getHours()
    m = dates.effectTime.getMinutes()
    s = dates.effectTime.getSeconds()
  }
  let loopType = dates.loopType
  var cron = ''
  if (loopType === 'DAILY') {
    cron = s + ' ' + m + ' ' + h + ' * * ? *'
  } else if (loopType === 'WEEKLY') { // 星期天为1，星期6为7
    cron = s + ' ' + m + ' ' + h + ' ? * ' + w.join(',')
  } else if (loopType === 'MONTHLY') { // 1-31
    cron = s + ' ' + m + ' ' + h + ' ' + mo.join(',') + ' * ? *'
  }
  return cron
}
